module.exports = function(grunt) {

    // Project configuration.
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        jshint: {
            options: {
                newcap: false
            },
            all: ['Gruntfile.js', 'public/javascripts/libraries/**/*.js', 'public/javascripts/app.js', 'public/javascripts/main.js', 'public/javascripts/module.js', 'public/javascripts/proMain.js'],
            min: ['public/minjs/**/*.js']
        },
        uglify: {
            options: {
                banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n' //添加banner
            },
            build: {
                options: {
                    mangle: {
                        except: ['require', 'define'] //seajs and require 必须不能混淆这两个函数
                    }
                },
                files: [{
                    expand: true,
                    cwd: 'public/javascripts',
                    src: ['**/*.js', '!**/*.min.js'],
                    dest: 'public/js'
                }]
            }
        },
        concat: {
            ej: {
                files: {
                    'public/lib/chart/ej.js': ['public/lib/chart/ej.core.min.js', 'public/lib/chart/ej.data.min.js', 'public/lib/chart/ej.chart.min.js'],
                    'public/lib/angularall/angular.js': ['public/lib/angular/angular.min.js', 'public/lib/angular/*.js', 'public/lib/angular-ui/*.min.js']
                }
            }

        },
        cssmin: {
            minify: {
                expand: true,
                cwd: 'public/stylesheets/',
                src: ['*.css', '!*.min.css'],
                dest: 'public/stylesheets/min/',
                ext: '.min.css'
            },
            combine: {
                files: {
                    'public/stylesheets/style.min.css': [
                        'public/stylesheets/min/leaflet.min.css',
                        'public/stylesheets/min/semantic.min.css',
                        'public/stylesheets/min/site.min.css',
                        'public/stylesheets/min/metro.min.css'
                    ],
                }
            }
        },
        groc: {
            javascript: [
                "public/javascripts/app/**/*.js", "README.md"
            ],
            options: {
                "out": "doc/"
            }
        }
    });

    grunt.registerTask('mani', 'My Task to change manifest file version Number', function() {
        var done = this.async();
        var path = require('path'),
            fs = require('fs');
        var manifest_path = path.join(__dirname, './public/manifest.appcache');
        fs.readFile(manifest_path, function(err, data) {
            if (err) {
                console.log(err);
            }
            var str = data.toString();
            var reg1 = /#.+/;
            var str1 = str.match(reg1)[0];
            var rep1 = "# " + new Date().toString();

            var reg2 = /# version.+/;
            var str2 = str.match(reg2)[0];
            var rep2 = "# version:" + (parseFloat(str2.split(':')[1]) + 0.0001).toString();
            str = str.replace(str1, rep1);
            str = str.replace(str2, rep2);
            fs.writeFile(manifest_path, str, function(err) {
                done();
            });
        });
    });

    // Load the plugin that provides the "uglify" task.
    grunt.loadNpmTasks('grunt-contrib-cssmin');
    grunt.loadNpmTasks('grunt-contrib-jshint');
    // Default task(s).
    grunt.registerTask('default', ['cssmin:minify', 'cssmin:combine', 'mani']);
    grunt.registerTask('hint', ['jshint:all']);
};
